<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="keywords" content="">
  <meta name="description" content="">
  <title>复制流程</title>
  <link href="/static/css/bootstrap.min.css" rel="stylesheet"/>
  <link rel="stylesheet" href="/static/include/css/style.css">
  <link href="/static/css/portlet.css" rel="stylesheet"/>
  <style>
    .delete-trans{
      font-size: 20px;
      line-height: 34px;
      cursor: pointer;
    }
  </style>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">

  <div class="row">
    <form id="copyPktForm" class="form-horizontal clear" method="post">
      <input type="hidden" name="flowId" field="flowId">
      <input type="hidden" name="systemCode" field="systemCode">
      <input type="hidden" name="groupCode" field="groupCode">
      <div class="form-group">
        <label class="control-label col-sm-3 is-required">项目名称</label>
        <div class="col-sm-8" >
          <input class="form-control" type="text" name="systemName" field="systemName" disabled>
        </div>
      </div>

      <div class="form-group">
        <label class="control-label col-sm-3 is-required">服务名称</label>
        <div class="col-sm-8">
          <input class="form-control" type="text" name="groupName" field="groupName" disabled>
        </div>
      </div>

      <div class="form-group">
        <label class="control-label col-sm-3 is-required">交易码</label>
        <div class="col-sm-4">
          <input class="form-control" name="transCodes" type="text">
        </div>
      </div>

    </form>
    <div class="btn-group-sm" id="toolbar" style="margin-left: 150px">
      <a class="btn btn-primary"  onclick="addTransCode()">
        <i class="fa fa-plus"></i> 新建
      </a>
    </div>
  </div>

</div>
<script src="/static/include/js/footer.js"></script>
<script>
  $(function () {
    var id = getQueryString('id')
    $.operate.get(api + '/comm/syscommflow/'+id, function (res) {
      if (res.code == 0) {
        applyDrawing('#copyPktForm',res.data)
      }
    }, false)

    $("#copyPktForm").validate({
      rules: {
        transCodes: {
          required: true
        },
      },
      messages: {
        transCodes: {
          required: "请输入交易码"
        },
      },
      focusCleanup: true
    });

  })

  var codeCount = 0
  var codeList = []
  function addTransCode() {
    codeCount++;
    $('#copyPktForm').append('<div class="form-group addCodeBox">' +
      '<label class="control-label col-sm-3 is-required">交易码'+codeCount+'</label>' +
      '<div class="col-sm-4">' +
      '<input class="form-control" type="text" name="transCodes'+codeCount+'" id="codeRule'+codeCount+'">' +
      '</div>' +
      '<i class="fa fa-minus-circle text-danger delete-trans" onclick="deleteCode(this)"></i>' +
      '</div>'
    )
    codeList.push("codeRule"+codeCount)
    $("#codeRule"+codeCount).rules("add", {
      required : true,
      messages : {
        required : "请输入交易码"
      }
    });
  }
  function deleteCode(e) {
    $(e).parents('.addCodeBox').remove()
    var thisId = $(e).parents('.addCodeBox').find('input').attr('id')
    // $('#'+thisId).rules("remove")
    codeList.forEach(function (item,index) {
      if (item == thisId){
        codeList.splice(index, 1)
      }
    })
  }

  function submitHandler() {
    if ($.validate.form()) {
      var data = $('#copyPktForm').serializeObject()
      data.systemName = $('input[name=systemName]').val()
      data.groupName = $('input[name=groupName]').val()
      data.transCodes = []
      data.transCodes.push($('input[name=transCodes]').val())
      codeList.forEach(function (item) {
        if ($("#"+item).val()){
          data.transCodes.push($("#"+item).val())
        }

      })
      // if (!Array.isArray(data.transCodes)) data.transCodes = [data.transCodes]

      $.ajax({
        url: api+'/comm/syscommflow/checkTransCode',
        data: JSON.stringify(data),
        headers:{
          'Content-Type': 'application/json;charset=UTF-8',
        },
        type: 'post',
        success: function (res) {
          if (res.code == 0){
            $.ajax({
              url: api+'/comm/syscommflow/copy',
              data: JSON.stringify(data),
              headers:{
                'Content-Type': 'application/json;charset=UTF-8',
              },
              type: 'post',
              success: function (resZ) {
                if (resZ.code == 0){
                  $.modal.close()
                  window.parent.$.modal.msgSuccess('复制成功');
                  window.parent.$.table.refresh();
                }else{
                  $.modal.msgError(resZ.msg ? resZ.msg : '复制失败')
                }
              }
            });
          }else{
            $.modal.msgError(res.msg ? res.msg : '复制失败')
          }
        }
      });
      return false;
    }
  }
</script>
</body>
</html>
